/* Copyright (C) 2008 Sahid Ferdjaoui <sahid@funraill.org>
 *
 *   This file is part of Postmemd.
 *
 * Postmemd is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "pm_atom.h"

#include "uthash.h"

#include "pm_log.h"

#include <string.h>
#include <stdlib.h>

extern atom_t * atoms;


int
pm_atom_init (atom_t ** atom)
{
	if (( * atom = malloc (sizeof (atom_t))) == NULL)
		{
			pm_log_error ("malloc () - new atom", PM_NOPRINT_ERRNO);
			return -1;
		}
	
	( * atom)->data = NULL;

	return 0;
}


int
pm_atom_add (atom_t * atom)
{
	
	HASH_ADD_STR (atoms, key, atom);
	return 0;
}


int
pm_atom_get (const char key[], atom_t ** atom)
{
	( * atom) = NULL;
	HASH_FIND_STR (atoms, key, ( * atom));
	if (!( * atom))
		return -1;
	return 0;
}

int
pm_atom_del (const char key[])
{
	atom_t * atom = NULL;

	HASH_FIND_STR (atoms, key, atom);
	if (!atom)
		return -1;
	
	HASH_DEL (atoms, atom);
	return 0;
}
